// utils/version-checker.js

const versionChecker = {
  // 比较版本号
  compareVersion(v1, v2) {
    const v1Parts = v1.split('.');
    const v2Parts = v2.split('.');
    
    for (let i = 0; i < v1Parts.length; ++i) {
      const v1Num = parseInt(v1Parts[i]);
      const v2Num = parseInt(v2Parts[i]);
      if (v1Num > v2Num) return 1;
      if (v1Num < v2Num) return -1;
    }
    return 0;
  },

  // 检查更新
  async checkUpdate() {
    // 获取当前应用版本号
    const currentVersion = plus.runtime.version;
    
    try {
      const res = await uni.request({
        url: 'https://your-api.com/check-update',
        method: 'POST',
        data: {
          version: currentVersion,
          platform: 'android'
        }
      });
      
      const serverData = res.data;
      
      if (this.compareVersion(serverData.version, currentVersion) > 0) {
        return {
          hasUpdate: true,
          ...serverData
        };
      }
      
      return {
        hasUpdate: false
      };
      
    } catch (e) {
      console.error('检查更新失败:', e);
      return {
        hasUpdate: false,
        error: e
      };
    }
  }
};

export default versionChecker;